import React, {Component} from 'react';
import {Form, Input, Modal, message} from 'antd';
import {reqCreateSpiderTask, reqUpdateSpiderTask} from "../../../../api/tasks";

// 首页
export default class ModalForm extends Component {

  state = {
    confirmLoading: false
  }

  layout = {
    labelCol: {
      span: 5,
    },
  };

  // 确认添加
  // 新增一个任务
  handleOk = () => {
    this.form.validateFields().then(values => {
      this.setState({
        confirmLoading: true
      })
      const { formData } = this.props;
      if (formData && Object.keys(formData).length > 0) {
        reqUpdateSpiderTask(Object.assign(values, {_id: this.props.formData._id})).then(res => {
          const data = res.data;
          if (data.code === 0) {
            this.props.closeDialog(false, 'update');
            message.success('更新成功！');
          } else {
            message.error('更新失败！');
          }
          this.setState({
            confirmLoading: false
          })
        }).catch(() => {
          this.setState({
            confirmLoading: false
          })
        })
      } else {
        reqCreateSpiderTask(values).then(res => {
          const data = res.data;
          if (data.code === 0) {
            this.props.closeDialog(false, 'add');
            message.success('添加成功！');
          } else {
            message.error('添加失败！');
          }
          this.setState({
            confirmLoading: false
          })
        }).catch(() => {
          this.setState({
            confirmLoading: false
          })
        })
      }
    }).catch(errInfo => {
    })
  }

  // 关闭对话框
  handleCancel = () => {
    this.props.closeDialog(false);
  }

  render() {
    const {isModalVisible, formData} = this.props;
    const {confirmLoading} = this.state;
    return (
      <Modal title="添加"
             confirmLoading={confirmLoading}
             destroyOnClose={true}
             visible={isModalVisible}
             onOk={this.handleOk}
             onCancel={this.handleCancel}>
        <Form
          {...this.layout}
          name="basic"
          ref={instance => this.form = instance}
          initialValues={formData}
          preserve={false}
        >
          <Form.Item
            label="任务名称"
            name="name"
            rules={[
              {
                required: true,
                message: '请输入任务名称',
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="文件路径"
            name="path"
            rules={[
              {
                required: true,
                message: '请输入文件路径',
              },
            ]}
          >
            <Input />
          </Form.Item>
          <Form.Item label="运行命令"
                     name="command"
                     rules={[
                       {
                         required: true,
                         message: '请输入运行命令'
                       }
                     ]}
          >
            <Input />
          </Form.Item>
        </Form>
      </Modal>
    )
  }
}
